﻿using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;

namespace CardPlay.Client.Converters
{
    public class VisibiltyToBoolConverter : IValueConverter
    {
        public VisibiltyToBoolConverter()
        {
            Not = false;
            Inverted = false;
        }

        public bool Inverted { get; set; }
        public bool Not { get; set; }

        private object VisibilityToBool(object value)
        {
            return (value is Visibility) 
                ? (((Visibility) value) == Visibility.Visible) ^ Not 
                : DependencyProperty.UnsetValue;
        }

        private object BoolToVisibility(object value)
        {
            return (value is bool) 
                ? (((bool) value ^ Not) ? Visibility.Visible : Visibility.Collapsed) 
                : DependencyProperty.UnsetValue;
        }


        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return Inverted ? BoolToVisibility(value) : VisibilityToBool(value);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return Inverted ? VisibilityToBool(value) : BoolToVisibility(value);
        }
    }
}